<?php
namespace app\admin\controller;
use app\admin\controller\Base;
class City extends Base
{
    private $obj;
    
    public function _initialize() {
        $this->obj=model("City");
    }
    
    //城市分类列表
    public function index(){
        $parentId=input("get.parent_id",0,'intval');
        $citys=$this->obj->getFirstCitys($parentId);
        
        return $this->fetch("",[
            "citys"=>$citys
        ]);
    }
    
    //添加操作
    public function add(){
        $citys=$this->obj->getNormalFirstCity();
        return $this->fetch("add",[
           "citys"=>$citys 
        ]);
    }
    //保存利用tp5中save特性
    public function  save(){
        if(!request()->isPost()){
            $this->error("非法请求");
        }
        $data=input("post.");//表单传值
        $validate= validate("City");
        if(!$validate->scene("add")->check($data)){
            $this->error($validate->getError());
        }
        
        //执行更新
        if(!empty($data['id'])){
            
            $this->update($data);
        }
 
        //将数据提交给model层进行处理
        $res=$this->obj->add($data);
        if($res){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }

    }
    
    //状态*删除
    public function status(){
        if(!request()->isGet()){
            $this->error("非法请求");
        }
        //get传值
        $data=input("get.");
        $validate= validate('City');
        if(!$validate->scene("status")->check($data)){
            $this->error($validate->getError());
        }
       $res=$this->obj->save(["status"=>$data["status"]],["id"=>$data['id']]);
        if($res){
            $this->success("更新成功");
        }else{
            $this->error("更新失败");
        }
    }
    //是否默认
    public function Isdefault(){
         if(!request()->isGet()){
            $this->error("非法请求");
        }
        //get传值
        $data=input("get.");
        $validate=validate("City");
        if(!$validate->scene("default")->check($data)){
            $this->error($validate->getError());
        }
        
       $res=$this->obj->save(['is_default'=>$data['is_default']],["id"=>$data['id']]);
        if($res){
            $this->success("更新成功");
        }else{
            $this->error("更新失败");
        }
        
    }
    
    
    //修改
    public function edit($id=0){
     if(intval($id)<0){
         $this->error("参数不准确");
      }   
      $city=$this->obj->get($id);
      $citys=$this->obj->getNormalFirstCity();
      
      return $this->fetch("",[
          "city"=>$city,
          "citys"=>$citys,
      ]);  
    }
    //更新数据
    public function update($data){
        
       $res=$this->obj->save($data,["id"=>intval($data['id'])]);
        if($res){
            $this->success("更新成功");
        }else{
            $this->error("更新失败");
        }
        
    }
    
    //排序
    public function listorder($id, $listorder){
        $res=$this->obj->save(["listorder"=>$listorder],["id"=>$id]);
        if($res){
            $this->result($_SERVER['HTTP_REFERER'],1,'success');
        }else{
            $this->result($_SERVER['HTTP_REFERER'],0,'更新失败');
        }
        
    }
    
    
}
